#include "camera_fps.hh"

CameraFps::CameraFps (const Vector3f& pos, Window* w)
  : Camera (pos, w)
{
  calc_vect ();
}

CameraFps::~CameraFps ()
{}

void
CameraFps::move (const Vector3f& val)
{
  Vector3f fwd = forward_ * (time_ / 20.0);
  Vector3f lft = left_ * (time_ / 20.0);

  if (val (2) == 1)
    pos_ += fwd;
  else if (val (2) == -1)
    pos_ -= fwd;

  if (val (1) == 1)
    pos_ -= lft;
  else if (val (1) == -1)
    pos_ += lft;

  if (val (0) != 0)
    pos_ (0, pos_ (0) - val (0));

  Vector3f tmp;
  tmp = forward_;
  tmp += pos_;
  target_ = tmp;
}

void
CameraFps::calc_vect ()
{
  Vector3f up;

  up.val_set (1, 0, 0);

  if (teta > 89)
    teta = 89;
  else if (teta < -89)
    teta = -89;

  float r_temp = cos (teta * M_PI / 180);

  forward_ (2, r_temp * cos (phi * M_PI / 180));
  forward_ (1, r_temp * sin (phi * M_PI / 180));
  forward_ (0, sin (teta * M_PI / 180));

  Vector3f le (up.vectorial (forward_));
  left_ = le;

  // normalize the vectors
  left_ /= left_.get_norme ();
  forward_ /= forward_.get_norme ();
  up_ = forward_.vectorial (left_);
  up_ /= up_.get_norme ();

  Vector3f tmp;
  tmp = pos_;
  tmp += forward_;
  target_ = tmp;
}

void
CameraFps::set_angles (float val_x, float val_y)
{
  teta -= val_y * 200;
  phi += val_x * 400;

  calc_vect ();
}

Vector3f*
CameraFps::get_3D_point (int x, int y)
{
  Vector3f vect_x = left_ * -x_val;
  Vector3f vect_y = up_ * y_val;

  Vector3f* res = new Vector3f (pos_);

  (*res) += forward_;

  (*res) += (vect_x * ((float) x) / 100.0);
  (*res) += (vect_y * ((float) y) / 100.0);

  return res;
}
